package com.abigdreamer.swing.app.skin;

import com.abigdreamer.swing.component.UIUtil;
import craky.componentc.JCDialog;
import craky.componentc.JCFrame;
import craky.util.Config;
import java.awt.Image;
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;
import java.util.TreeMap;

public class SkinManager
{
  private static Skin currentSkin;
  private static Skin currentPreviewSkin;
  private static Map<String, Skin> skinMap;
  private static Config config = new Config(SkinConst.CONFIG_PATH);
  private static JCFrame owner;
  
  public static void initSkin()
  {
    changeSkin(getSkin(config.getProperty("Skin_Name", "默认皮肤")), true);
  }
  
  public static void changeSkin(Skin newSkin, boolean save)
  {
    boolean currentShow = currentPreviewSkin == newSkin;
    if (((!save) && (currentShow)) || ((save) && (currentSkin == newSkin))) {
      return;
    }
    if (newSkin == null) {
      newSkin = (Skin)skinMap.get("默认皮肤".toUpperCase());
    }
    Image blurImage;
    if (!currentShow)
    {
      Image image = newSkin.getImage();
      blurImage = newSkin.getBlurImage();
      BufferedImage bufferedBlurImage = blurImage == null ? null : UIUtil.toBufferedImage(blurImage, null);
      for (Window win : Window.getWindows()) {
        if (win.isDisplayable()) {
          if ((win instanceof JCFrame)) {
            ((JCFrame)win).setBackgroundImage(image, bufferedBlurImage);
          } else if ((win instanceof JCDialog)) {
            ((JCDialog)win).setBackgroundImage(image, bufferedBlurImage);
          }
        }
      }
      currentPreviewSkin = newSkin;
      image = null;
      blurImage = null;
    }
    if (save)
    {
      for (Skin skin : skinMap.values()) {
        skin.setSelected(skin == newSkin);
      }
      currentSkin = newSkin;
      config.savePropertie("Skin_Name", currentSkin.getName());
    }
    config.saveConfig();
  }
  
  public static void loadAllSkin()
  {
    skinMap = new TreeMap();
    Skin defaultSkin = new Skin("默认皮肤");
    skinMap.put(defaultSkin.getName().toUpperCase(), defaultSkin);
    File skinRoot = new File(SkinConst.SKIN_DIR);
    if ((skinRoot.exists()) && (skinRoot.isDirectory()))
    {
      File[] skinDirs = skinRoot.listFiles();
      

      int count = 0;
      for (File skinDir : skinDirs) {
        if (skinDir.isDirectory())
        {
          String name = skinDir.getName();
          String path = skinDir.getPath();
          
          File file = new File(path + SkinConst.FILE_SEP + "normal.png");
          if ((file.exists()) && (file.isFile()))
          {
            file = new File(path + SkinConst.FILE_SEP + "blur.png");
            if ((file.exists()) && (file.isFile()))
            {
              file = new File(path + SkinConst.FILE_SEP + "preview.png");
              if ((file.exists()) && (file.isFile()))
              {
                skinMap.put(name.toUpperCase(), new Skin(name));
                count++;
                if (count >= 13) {
                  break;
                }
              }
            }
          }
        }
      }
    }
  }
  
  public static Skin getCurrentSkin()
  {
    return currentSkin;
  }
  
  public static Skin getSkin(String name)
  {
    return (Skin)skinMap.get(name.toUpperCase());
  }
  
  public static Map<String, Skin> getAllSkins()
  {
    return skinMap;
  }
  
  public static void setOwner(JCFrame mainFrame)
  {
    owner = mainFrame;
  }
  
  public static JCFrame getOwner()
  {
    return owner;
  }
  
  public static void savePropertie(String key, String value)
  {
    config.savePropertie(key, value);
  }
  
  public static String getProperty(String key, String defaultValue)
  {
    return config.getProperty(key, defaultValue);
  }
}
